/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewQuestionDialog.java
 *
 * Created on Aug 25, 2013, 2:41:22 PM
 */

package frames;

import com.ap.logic.Classification.Category;
import com.ap.logic.QuizClasses.Question;
import com.ap.logic.xml.ReadWriteClassificationXML;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *
 * @author andrew
 */
public class NewQuestionDialog extends javax.swing.JDialog {

    /** Creates new form NewQuestionDialog */
    public NewQuestionDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
       initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        questionTypeLabel = new javax.swing.JLabel();
        questionTextLabel = new javax.swing.JLabel();
        questionAnswerLabel = new javax.swing.JLabel();
        saveQuestionButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        clearFormButton = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        questionTypeComboBox = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        questionTextTextPane = new javax.swing.JTextPane();
        previewButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        questionAnswerTextPane = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Question");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(new java.awt.Dimension(500, 450));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        questionTypeLabel.setText("Question Type");

        questionTextLabel.setText("Question Text");

        questionAnswerLabel.setText("Correct Answer");

        saveQuestionButton.setText("Save Question");
        saveQuestionButton.setMaximumSize(new java.awt.Dimension(650, 650));
        saveQuestionButton.setMinimumSize(new java.awt.Dimension(350, 350));
        saveQuestionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveQuestionButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        clearFormButton.setText("Clear Form");
        clearFormButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearFormButtonActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        questionTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "With Variants", "Yes/No Question", "Detailed Answer Question", "TIP" }));
        questionTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                questionTypeComboBoxActionPerformed(evt);
            }
        });
        questionTypeComboBox.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                questionTypeComboBoxPropertyChange(evt);
            }
        });

        questionTextTextPane.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jScrollPane2.setViewportView(questionTextTextPane);
        questionTextTextPane.getAccessibleContext().setAccessibleDescription("text/html");

        previewButton.setLabel("Preview");
        previewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previewButtonActionPerformed(evt);
            }
        });

        questionAnswerTextPane.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jScrollPane1.setViewportView(questionAnswerTextPane);
        questionAnswerTextPane.getAccessibleContext().setAccessibleDescription("text/html");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(questionTypeLabel)
                .addGap(40, 40, 40)
                .addComponent(questionTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(408, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(176, Short.MAX_VALUE)
                .addComponent(previewButton)
                .addGap(90, 90, 90)
                .addComponent(saveQuestionButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(clearFormButton)
                .addGap(10, 10, 10)
                .addComponent(cancelButton)
                .addGap(32, 32, 32))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(questionTextLabel)
                .addContainerGap(572, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(questionAnswerLabel)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(questionTypeLabel)
                    .addComponent(questionTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(questionTextLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(questionAnswerLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(clearFormButton)
                    .addComponent(saveQuestionButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(previewButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void previewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previewButtonActionPerformed
        //<img src='file://localhost/D:/career2.0.jpg'  style='width:100px;heigth:100px' />
    
        String questionText=this.questionTextTextPane.getText();
        this.questionTextTextPane.setContentType("text/html");
        this.questionTextTextPane.setText(questionText.replaceAll("\n", "&lt;br&gt;").replaceAll("&lt;", "<").replaceAll("&gt;", ">") );

        JOptionPane.showMessageDialog(this, questionText);

        String questionAnswer=this.questionAnswerTextPane.getText();
        this.questionAnswerTextPane.setContentType("text/html");
        this.questionAnswerTextPane.setText(questionAnswer.replaceAll("\n", "&lt;br&gt;").replaceAll("&lt;", "<").replaceAll("&gt;", ">") );

    }//GEN-LAST:event_previewButtonActionPerformed

    private void questionTypeComboBoxPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_questionTypeComboBoxPropertyChange
    }//GEN-LAST:event_questionTypeComboBoxPropertyChange

    private void questionTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_questionTypeComboBoxActionPerformed
        
        String selectedQuestionType=this.questionTypeComboBox.getSelectedItem().toString();

        if(selectedQuestionType.equals("With Variants")){
           

            this.questionTextTextPane.setContentType("text/html");
            this.questionTextTextPane.setText("");
            String variants="<br>$v$<br>"+
                                                        "$v0$ text of variant... $v0end$<br>"+
                                                        "$v1$  $v1end$<br>"+
                                                        "$v2$  $v2end$<br>"+
                                                        "$v3$  $v3end$<br>"+
                                                    "$vend$";
            this.questionTextTextPane.setText(variants);
             this.questionAnswerTextPane.setText("put here  number of variant (numeration from zero)");
        }

        if(selectedQuestionType.equals("Yes/No Question")){
           
            this.questionTextTextPane.setContentType("text/html");
            this.questionTextTextPane.setText("");
            // if yes no question in aswet put 'yes' or 'no'
            this.questionAnswerTextPane.setText("put here  'yes' or 'no'");
            
        }
        
        if(selectedQuestionType.equals("Detailed Answer Question")){
            
            this.questionTextTextPane.setContentType("text/html");
            this.questionTextTextPane.setText("");

            this.questionAnswerTextPane.setText("put here  detailed answer");
        }

       if(selectedQuestionType.equals("TIP")){
           
           this.questionTextTextPane.setContentType("text/html");
           this.questionTextTextPane.setText("");
           this.questionAnswerTextPane.setText("put here tip content");
       }

    }//GEN-LAST:event_questionTypeComboBoxActionPerformed


    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dialogResult=false;
        this.dispose();
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void clearFormButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearFormButtonActionPerformed
       this.questionTextTextPane.setText("");
       this.questionAnswerTextPane.setText("");
    }//GEN-LAST:event_clearFormButtonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        questionTypeComboBoxActionPerformed(null    );
    }//GEN-LAST:event_formWindowOpened

    private void saveQuestionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveQuestionButtonActionPerformed
        this.resultQuestion=new Question();

        String questionText=this.questionTextTextPane.getText().replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("<br>", "<br>\n")  ;
        String questionAnswer=this.questionAnswerTextPane.getText().replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("<br>", "<br>\n")  ;

         String selectedQuestionType=this.questionTypeComboBox.getSelectedItem().toString();
         //   System.err.println(questionAnswer);


         if(questionText.indexOf("<body>")>0){
            this.resultQuestion.setQuestionText(questionText.substring(questionText.indexOf("<body>")+6,questionText.indexOf("</body>")));
         }
         else{
             this.resultQuestion.setQuestionText(questionText);
         }

         if(questionAnswer.indexOf("<body>")>0){
            this.resultQuestion.setQuestionAnswer(questionAnswer.substring(questionAnswer.indexOf("<body>")+6,questionAnswer.indexOf("</body>")));
         }
         else{
             this.resultQuestion.setQuestionAnswer(questionAnswer);
         }


        if(selectedQuestionType.equals("With Variants")){
                this.resultQuestion.setType(0+"");
        }

        if(selectedQuestionType.equals("Yes/No Question")){
            this.resultQuestion.setType(1+"");
        }

        if(selectedQuestionType.equals("Detailed Answer Question")){
            this.resultQuestion.setType(2+"");
        }

       if(selectedQuestionType.equals("TIP")){
           this.resultQuestion.setType(9+"");
       }
         
        this.readerwriterQuestion.addQuestionToXML(this.resultQuestion, this.newQuestionCategory);
        this.dialogResult=true;
        this.dispose();
        this.setVisible(false);
    }//GEN-LAST:event_saveQuestionButtonActionPerformed




    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewQuestionDialog dialog = new NewQuestionDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton clearFormButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton previewButton;
    private javax.swing.JLabel questionAnswerLabel;
    private javax.swing.JTextPane questionAnswerTextPane;
    private javax.swing.JLabel questionTextLabel;
    private javax.swing.JTextPane questionTextTextPane;
    private javax.swing.JComboBox questionTypeComboBox;
    private javax.swing.JLabel questionTypeLabel;
    private javax.swing.JButton saveQuestionButton;
    // End of variables declaration//GEN-END:variables

    private  Question  resultQuestion=new Question();
    private  Category newQuestionCategory=null;


    private  Boolean dialogResult=false;
    private  ReadWriteClassificationXML readerwriterQuestion=new ReadWriteClassificationXML();

    
    /**
     * @param newQuestionCategory the newQuestionCategory to set
     */
    public void setNewQuestionCategory(Category newQuestionCategory) {
        this.newQuestionCategory = newQuestionCategory;
    }

    public Boolean getDialogResult(){
        return this.dialogResult;
    }
    
}
